package com.adxon;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import javax.servlet.http.HttpServletRequest;

public class Uri{
	private String ex;
	private String id;
	private String ph;
	private String st;
	private String tg;
	private String ty;

	public Uri(HttpServletRequest req)throws UnsupportedEncodingException{
		parse(URLDecoder.decode(req.getRequestURI(),"utf8"));
	}
	public Uri(String string){
		parse(string);
	}
	public boolean extEquals(String comp){
		if(ex==null)
			return false;
		return ex.equals(comp);
	}
	public String getExtension(){
		return ex;
	}
	public String getId(){
		return id;
	}
	public String getPath(){
		return ph;
	}
	public String getTags(){
		return tg;
	}
	public String getType(){
		return ty;
	}
	private void parse(String string){
		int b=0,c=0;
		try{
			if(string.charAt(0)=='/')
				b=c=1;
			int d=string.lastIndexOf('/'),e;
			if(d>b){
				c=d+1;
				id=string.substring(c);
				ph=string.substring(b,d);
			}else id=string.substring(b);
			d=id.indexOf('.');
			if(d>=0){
				e=id.lastIndexOf('.');
				if(d<e){
					ex=id.substring(e);
					ty=id.substring(d+1,e);
					id=id.substring(0,d);
				}else{
					ex=id.substring(e);
					id=id.substring(0,d);
				}
			}
		}catch(Exception e){}
		int d=id.length();
		if(d==0){
			id=null;
			if(c>b)
				c--;
		}else if(id.charAt(0)=='*'){
			tg=id.substring(1);
			id=null;
			c--;
		}else c+=d;
		st=string.substring(b,c);
	}
	public String toString(){
		return st;
	}
}